package com.example.graduation.controller;


import com.example.graduation.entity.Torder;
import com.example.graduation.form.IdAndCountForm;
import com.example.graduation.form.IdForm;
import com.example.graduation.form.SingleStringForn;
import com.example.graduation.form.StrIdForm;
import com.example.graduation.service.impl.ShoppingCartServiceImpl;
import com.example.graduation.util.R;
import com.example.graduation.vo.All;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyc
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/shopping-cart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartServiceImpl shoppingCartService;


    /**
     * @api {POST} /shopping-cart/buy 购买商品
     * @apiVersion 1.0.0
     * @apiGroup ShoppingCartController
     * @apiName buy
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=oilcij0y1
     * @apiParam (请求体) {Number} goodsId
     * @apiParam (请求体) {Number} count
     * @apiParamExample 请求体示例
     * {"goodsId":8834,"count":6130}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/buy")
    @ResponseBody
    public R buy(@RequestHeader String token, @RequestBody IdAndCountForm form) {
        Boolean buy = shoppingCartService.buy(token, form.getGoodsId(), form.getCount());
        return R.ok("购买成功");
    }


    /**
     * @api {POST} /shopping-cart/findOrderByToken 查询用户订单列表
     * @apiVersion 1.0.0
     * @apiGroup ShoppingCartController
     * @apiName findOrderByToken
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=w0ZXIdlE4
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {
     * "msg": "success",
     * "code": 200,
     * "order": [
     * {
     * "orderId": 1650811099087,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 2,
     * "createTime": 1650811099087,
     * "completeTime": null,
     * "total": 2.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 1650733212939,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 1,
     * "createTime": 1650733212939,
     * "completeTime": null,
     * "total": 1.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 1650733165885,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 2,
     * "createTime": 1650733165885,
     * "completeTime": null,
     * "total": 2.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 1650733134962,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 1,
     * "createTime": 1650733134962,
     * "completeTime": null,
     * "total": 1.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 1650733087027,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 1,
     * "createTime": 1650733087027,
     * "completeTime": null,
     * "total": 1.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 1650733066778,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 2,
     * "createTime": 1650733066778,
     * "completeTime": null,
     * "total": 2.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 1650732956809,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 2,
     * "createTime": 1650732957122,
     * "completeTime": null,
     * "total": 2.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 1650732776412,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 1,
     * "createTime": 1650732776732,
     * "completeTime": null,
     * "total": 1.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ]
     * },
     * {
     * "orderId": 213,
     * "userId": 17,
     * "shopId": 1,
     * "status": 1,
     * "price": 12.0,
     * "shuliang": 1,
     * "createTime": 2000,
     * "completeTime": 2000,
     * "total": 1.0,
     * "reduce": 0.0,
     * "freight": 0.0,
     * "leixin": 0,
     * "list": [
     * null
     * ]
     * }
     * ]
     * }
     */
    @PostMapping("/findOrderByToken")
    @ResponseBody
    public R findOrderByToken(@RequestHeader String token) {
        List<Torder> order = shoppingCartService.findOrderByToken(token);
        return R.ok().put("order", order);
    }


    /**
     * @api {POST} /shopping-cart/findShopOrder 查询商家订单信息
     * @apiVersion 1.0.0
     * @apiGroup ShoppingCartController
     * @apiName findShopOrder
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=M7PMuuv
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findShopOrder")
    @ResponseBody
    public R findShopOrder(@RequestHeader String token) {
        List<Torder> shopOrder = shoppingCartService.findShopOrder(token);
        return R.ok().put("shopOrder", shopOrder);
    }


    /**
     * @api {POST} /shopping-cart/selectOrderById 根据订单id查询订单详情
     * @apiVersion 1.0.0
     * @apiGroup ShoppingCartController
     * @apiName selectOrderById
     * @apiParam (请求体) {Number} shopId
     * @apiParamExample 请求体示例
     * {"shopId":6936}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {
     * "msg": "success",
     * "code": 200,
     * "torder": {
     * "orderId": 1650732776412,
     * "userId": 17,
     * "shopId": 7,
     * "status": 3,
     * "price": null,
     * "shuliang": 1,
     * "createTime": 1650732776732,
     * "completeTime": null,
     * "total": 1.0,
     * "reduce": null,
     * "freight": null,
     * "leixin": 0,
     * "list": [
     * {
     * "goodsId": 7,
     * "userId": 17,
     * "shopId": 7,
     * "count": 3,
     * "oldprice": 1.0,
     * "newprice": 1.0,
     * "type": "1",
     * "status": null,
     * "goodsname": "3we",
     * "createTime": 1650701359268,
     * "sale": null,
     * "month": 4,
     * "photo": "https://emos-1307380095.cos.ap-guangzhou.myqcloud.com/emos/17/1650701355123.jpg",
     * "des": "11",
     * "notice": "11"
     * }
     * ],
     * "user": null
     * }
     * }
     */
    @PostMapping("/selectOrderById")
    @ResponseBody
    public R selectOrderById(@RequestBody StrIdForm form) {
        Torder torder = shoppingCartService.selectOrderById(form.getOrderId());
        return R.ok().put("torder", torder);
    }


    /**
     * @api {POST} /shopping-cart/buyGoods 购买商品测试版
     * @apiVersion 1.0.0
     * @apiGroup ShoppingCartController
     * @apiName buyGoods
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=0j0ZYPKMx
     * @apiParam (请求体) {String} info
     * @apiParamExample 请求体示例
     * {"info":"80tyHiSBjZ"}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/buyGoods")
    @ResponseBody
    public R buyGoods(@RequestHeader String token, @RequestBody SingleStringForn forn) {
        shoppingCartService.buyGoods(token, forn.getInfo());
        return R.ok();
    }


    /**
     * @api {POST} /shopping-cart/findRecord 统计销售
     * @apiVersion 1.0.0
     * @apiGroup ShoppingCartController
     * @apiName findRecord
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=Z4us
     * @apiParam (请求体) {Number} shopId
     * @apiParamExample 请求体示例
     * {"shopId":6698}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findRecord")
    @ResponseBody
    public R findRecord(@RequestHeader String token, @RequestBody IdForm form) {
        List<All> record = shoppingCartService.findRecord(form.getShopId(), token);
        return R.ok().put("record", record);
    }

}

